package com.igoal.dto;

import java.util.Date;

import com.igoal.entity.Comment;
import com.igoal.entity.ComponentEnum;
import com.igoal.entity.User;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;
import com.igoal.web.model.DummySettings;

public class CommentDTO extends BaseDTO<Comment> {

	private String title;
	private String guestName;
	private String guestEmail;
	private Long componentId;
	private ComponentEnum component;
	private String comment;
	private Long ownerId;
	private String ownerName;
	private String avatarURL;

	public Long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}

	public String getOwnerName() {
		return ownerName;
	}

	public void setOwnerName(String ownerName) {
		this.ownerName = ownerName;
	}

	public String getAvatarURL() {
		return avatarURL;
	}

	public void setAvatarURL(String avatarURL) {
		this.avatarURL = avatarURL;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getGuestName() {
		return guestName;
	}

	public void setGuestName(String guestName) {
		this.guestName = guestName;
	}

	public String getGuestEmail() {
		return guestEmail;
	}

	public void setGuestEmail(String guestEmail) {
		this.guestEmail = guestEmail;
	}

	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}

	public ComponentEnum getComponent() {
		return component;
	}

	public void setComponent(ComponentEnum component) {
		this.component = component;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "body")
	public void initCommentBody(Object body) {
		if (body != null) {
			this.comment = (String) body;
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "user")
	public void initUserFromEntity(Object entity) {
		if (entity != null && entity instanceof User) {
			User user = (User) entity;

			this.ownerId = user.getId();
			this.ownerName = user.getUsername();
			if (user.getAvatar() != null) {
				this.avatarURL = user.getAvatar();
			} else {
				// TODO: remove this dummy setting after using default image
				// checker
				this.avatarURL = DummySettings.DEFAULT_AVATAR_URL;
			}
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "updateDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updateDate = date.getTime();
			this.updatedSince = CalendarUtils.calculateMinutesDifferentBeforeNow(date);
		}
	}
	
	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.TO, value = "updateDate")
	public Date initUpdateDate(){
		return new Date();
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.TO, value = "body")
	public String copyCommentBody() {
		return this.comment;
	}
}
